setwd("E://OneDrive//1-教学//科技论文写作//课件//课上使用的文档//2.9.5-科研绘图//柱形图")

library(xlsx)
library(ggplot2)
library(RColorBrewer)

data1 <- read.xlsx("示例数据.xlsx", sheetName = "Sheet1", encoding = "UTF-8")
data1

ggplot(data1)+
  geom_col(aes(x = Conc, y = mean))

#图片x轴10%于1%和2%之间，可使用因子排序
data1$Conc <- factor(data1$Conc, levels = c("0%", "1%", "2%", "5%", "10%"))

ggplot(data1)+
  geom_col(aes(x = Conc, y = mean))

  
ggplot(data1)+
  geom_col(aes(x = Conc, y = mean))+
  geom_errorbar(aes(x = Conc, ymin = mean - sd, ymax = mean + sd))

ggplot(data1)+
  geom_col(aes(x = Conc, y = mean))+
  geom_errorbar(aes(x = Conc, ymin = mean - sd, ymax = mean + sd))+
  geom_text(aes(x = Conc, y = mean + sd + 0.3, label = sig.))

ggplot(data1)+
  geom_col(aes(x = Conc, y = mean))+
  geom_errorbar(aes(x = Conc, ymin = mean - sd, ymax = mean + sd))+
  geom_text(aes(x = Conc, y = mean + sd + 0.3, label = sig.))+
  labs(x = "Concentration", y = "Biomass (g)")


ggplot(data1)+
  geom_col(aes(x = Conc, y = mean), width = 0.5)+
  geom_errorbar(aes(x = Conc, ymin = mean - sd, ymax = mean + sd), width = 0.1)+
  geom_text(aes(x = Conc, y = mean + sd + 0.3, label = sig.))+
  labs(x = "Concentration", y = "Biomass (g)")+
  theme_bw()

#############################################
data2 <- read.xlsx("示例数据.xlsx", sheetName = "Sheet2", encoding = "UTF-8")
data2
data2$Conc <- factor(data2$Conc, levels = c("0%", "1%", "2%", "5%", "10%"))

ggplot(data2)+
  geom_col(aes(x = Conc, y = mean), width = 0.5)+
  geom_errorbar(aes(x = Conc, ymin = mean - sd, ymax = mean + sd), width = 0.1)+
  geom_text(aes(x = Conc, y = mean + sd + 0.3, label = sig.))+
  labs(x = "Concentration", y = "Biomass (g)")+
  theme_bw() #柱高为累加值，误差线及显著性标记为单独标记


ggplot(data2)+
  geom_col(aes(x = Conc, y = mean, fill = MPs), width = 0.5)+ #填充颜色区分微塑料种类
  geom_errorbar(aes(x = Conc, ymin = mean - sd, ymax = mean + sd), width = 0.1)+
  geom_text(aes(x = Conc, y = mean + sd + 0.3, label = sig.))+
  labs(x = "Concentration", y = "Biomass (g)")+
  theme_bw()


ggplot(data2)+
  geom_col(aes(x = Conc, y = mean, fill = MPs), position = "dodge")+ #由堆叠改并列
  geom_errorbar(aes(x = Conc, ymin = mean - sd, ymax = mean + sd, color = MPs), width = 0.1)+
  geom_text(aes(x = Conc, y = mean + sd + 0.3, label = sig., color = MPs))+
  labs(x = "Concentration", y = "Biomass (g)")+
  theme_bw()


ggplot(data2)+
  geom_col(aes(x = Conc, y = mean, fill = MPs), width = 0.75, position = position_dodge())+
  geom_errorbar(aes(x = Conc, ymin = mean - sd, ymax = mean + sd, color = MPs), position = position_dodge(.75), width = 0.1)+
  geom_text(aes(x = Conc, y = mean + sd + 0.3, label = sig., color = MPs), position = position_dodge(.75))+
  labs(x = "Concentration", y = "Biomass (g)")+
  theme_bw()


ggplot(data2)+
  geom_col(aes(x = Conc, y = mean, fill = MPs), width = 0.75, position = position_dodge2(preserve = "single"))+ #position_dodge2 不分层& preserve = "single"统一单柱宽度
  geom_errorbar(aes(x = Conc, ymin = mean - sd, ymax = mean + sd, color = MPs), position = position_dodge(.75), width = 0.1)+
  geom_text(aes(x = Conc, y = mean + sd + 0.3, label = sig., color = MPs), position = position_dodge(.75))+
  labs(x = "Concentration", y = "Biomass (g)")+
  scale_color_manual(values = brewer.pal(8,"Dark2"))+
  scale_fill_manual(values = brewer.pal(8,"Dark2"))+
  theme_bw()

Fig <- ggplot(data2)+
  geom_col(aes(x = Conc, y = mean, fill = MPs), width = 0.75, position = position_dodge2(preserve = "single"))+ #position_dodge2 不分层& preserve = "single"统一单柱宽度
  geom_errorbar(aes(x = Conc, ymin = mean - sd, ymax = mean + sd, color = MPs), position = position_dodge(.75), width = 0.1)+
  geom_text(aes(x = Conc, y = mean + sd + 0.3, label = sig., color = MPs), position = position_dodge(.75))+
  labs(x = "Concentration", y = "Biomass (g)")+
  scale_color_manual(values = brewer.pal(8,"Dark2"))+
  scale_fill_manual(values = brewer.pal(8,"Dark2"))+
  theme_bw()

ggsave("Fig.svg", plot = Fig, width = 6, height = 4, bg = "white")
